データフレームの結合
# 結合例1
A <- data.frame(id = 1:5, varA = 1:5)
B <- data.frame(id = 1:5, varB = c(0, 0, 1, 1, 1))
C <- data.frame(id = c(1, 3), varC = c(10, 20))
merge(A, B, by = "id")
## id varA varB
## 1 1 1 0
## 2 2 2 0
## 3 3 3 1
## 4 4 4 1
## 5 5 5 1
merge(A, C, by = "id")
## id varA varC
## 1 1 1 10
## 2 3 3 20
merge(A, C, by = "id", all = T) # NAで埋めて全て表示
## id varA varC
## 1 1 1 10
## 2 2 2 NA
## 3 3 3 20
## 4 4 4 NA
## 5 5 5 NA
# 結合例2
D <- data.frame(id = c(1, 2, NA, 4, 5), varD = 1:5)
E <- data.frame(id = c(1, 2, 3, 4, NA), varD = letters[1:5])
merge(D, E, by = "id")
## id varD.x varD.y
## 1 1 1 a
## 2 2 2 b
## 3 4 4 d
## 4 NA 3 e
merge(D, E, by = "id", incomparables = NA) # NAを結合キーから除外
## id varD.x varD.y
## 1 1 1 a
## 2 2 2 b
## 3 4 4 d